'use strict';

const Controller = require('egg').Controller;

class FavaController extends Controller {
  async video() {
    const { ctx } = this
    const user_id = ctx.authUser.id
    ctx.validate({ video_id: { type: 'int', required: true, desc: '作品id' } })
    const { video_id } = ctx.request.body
    const fava = await ctx.model.Fava.findOne({
      where: {
        user_id,
        video_id
      }
    })
    if (fava) {
      fava.destroy()
      return ctx.apiSuccess({
        status: false,
        msg: '取消收藏成功'
      })
    }
    const video = await ctx.model.Video.findOne({
      where: {
        id: video_id
      }
    })
    if (!video) {
      return ctx.apiFail('视频不存在')
    }
    await ctx.model.Fava.create({
      user_id,
      video_id
    })
    ctx.apiSuccess({
      status: true,
      msg: '收藏成功'
    })
  }

  async list() {
    const { ctx } = this
    const user_id = ctx.query.user_id
    ctx.validate({user_id:{required:true,type:'int',desc:'用户id'}})
    let rows = await ctx.page('Fava', {
      user_id
    }, {
      include: [{
        model: ctx.model.Video
      }]
    });
    console.log(rows)
    rows = rows.map(item => {
      return {
        created_time: item.video.created_time,
        id: item.video.id,
        title: item.video.title,
        cover: item.video.cover,
        category_id: item.video.category_id,
        user_id: item.video.user_id,
        duration: item.video.duration,
        desc: item.video.desc,
        play_count: item.video.play_count,
        danmu_count: item.video.danmu_count,
      }
    });

    ctx.apiSuccess(rows)

  }
}

module.exports = FavaController;
